/**
 * The handler package.
 */
package fr.umlv.m2.ig.handler;

import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * This classes defines the XML handler associated to the XML color file.
 * 
 * @author Gambouele Bertrand.
 * @author Viravout Alexandre.
 */
public class XMLColorHandler extends DefaultHandler {
  
	/**
	 * Map which associate an string identifier to a {@code Color} object.
	 */
	private final Map<String, Color> colors;

	/**
	 * Creates a new {@code XMLColorHandler} object.
	 */
	public XMLColorHandler() {
		this.colors = new HashMap<String, Color>();
	}
	
	/**
	 * Gets a map of colors identified by a string identifier.
	 * 
	 * @return a map of colors.
	 */
	public Map<String, Color> getColors() {
		return this.colors;
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if ("color".equals(qName)) {
			int red = Integer.valueOf(attributes.getValue("r"));
			int green = Integer.valueOf(attributes.getValue("g"));
			int blue = Integer.valueOf(attributes.getValue("b"));
			Color c = new Color(red, green, blue);
			
			this.colors.put(attributes.getValue("id"), c);
		}
	}
}
